import { Component, OnInit} from '@angular/core';
import {App, NavController} from 'ionic-angular';
import {UserService} from "../../../providers/user-service";
import {AppPopup} from "../../../util/popup";
import {services} from "../../../providers/service";
import {ModifyUser} from "../modify-user/modify-user";
import {UserLogin} from "../user-login/user-login";

@Component({
    selector: 'user-detail',
    templateUrl: 'user-detail.html',
})
export class UserDetail implements OnInit {
    personalDetail = {
        "companyAddressId": "",
        "companyAddressName": "",
        "companyId": "",
        "companyName": "",
        "id": "",
        "mobile": "",
        "name": "",
        "type": "0",
        "companyMemberLimit": "",
        "companyMobile": ""
    };

    area: string;            //省市区地址

    showAlert = true;       //控制弹框的显隐
    name;                   //子账号修改姓名
    nameNull = false;

    constructor(public navCtrl: NavController,
                private popup: AppPopup,
                private app: App,
                public service: services,
                private userServ: UserService) {}

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(res => {
                console.info("用户详情获取成功：", res);
                this.personalDetail = res.infoData;
                let areaId = res.infoData.companyAddressId;
                this.area = areaId ? this.service.compareCode(areaId).join("") : "";
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }
            });
    }

    ngOnInit() {

    }

    modifyUserDetail() {
        this.navCtrl.push(ModifyUser);
    }


    goBack() {
        this.navCtrl.pop();
    }

    showPopup() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
    }

    //修改姓名 -- 保存
    btnChoose() {
        console.log(this.personalDetail)
        if (!this.name) {
            this.nameNull = true;
            return;
        } else {
        this.userServ.editChildAccountName({name: this.name})
            .then(data => {
                console.info("btnChoose，修改子账号用户名成功",data);
                this.personalDetail.name = this.name;
                this.nameNull = false;
                this.name = '';
                this.showAlert = true;
            })
            .catch(err => {
                console.error("btnChoose，修改子账号用户名失败：",err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.navCtrl.setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }
            });
        }
    }
}